<template>
  <div class="ruleChain-tools">
    <div class="scale-bar tool-item" v-if="zoom">
      <svg-icon icon-class="zoomDown" :className="`${zoomDownEnable ? '' : 'disabled'} icon`" @click="graphZoom('down')"/>
      <div class="split-line"></div>
      <div class="zoom-value" @dblclick="graphZoom('reset')">{{ zoomValue }}%</div>
      <div class="split-line"></div>
      <svg-icon icon-class="zoomUp" :className="`${zoomUpEnable ? '' : 'disabled'} icon`" @click="graphZoom('up')"/>
    </div>
    <div class="history-bar tool-item" v-if="history">
      <svg-icon icon-class="before" :className="`${undoEnable ? '' : 'disabled'} icon`" @click="historyHandler('undo')"/>
      <div class="split-line"></div>
      <svg-icon icon-class="after" :className="`${redoEnable ? '' : 'disabled'} icon`" @click="historyHandler('redo')"/>
    </div>
  </div>
</template>

<script>
export default {
  name: 'RuleChainTools',
  data() {
    return {
      zoomValue: 100,
      zoomUpEnable: true,
      zoomDownEnable: true,
      undoEnable: false,
      redoEnable: false
    }
  },
  props: {
    graph: {
      required: true
    },
    zoom: {
      type: Boolean,
      default: true
    },
    history: {
      type: Boolean,
      default: true
    }
  },
  mounted() {
    this.zoomValue = parseInt(this.graph.zoom() * 100)
    this.addEvent()
  },
  methods: {
    graphZoom(type) {
      if (type === 'down' && this.zoomDownEnable) {
        this.zoomValue -= 10
      }
      if (type === 'up' && this.zoomUpEnable) {
        this.zoomValue += 10
      }
      if (type === 'reset') {
        this.zoomValue = 100
      }
      this.graph.zoomTo((this.zoomValue / 100).toFixed(1) + '01')
    },
    historyHandler(type) {
      if (type === 'undo' && this.undoEnable) {
        this.graph.undo()
      }
      if (type === 'redo' && this.redoEnable) {
        this.graph.redo()
      }
    },
    addEvent() {
      this.graph.on('scale', () => {
        this.zoomValue = parseInt(this.graph.zoom() * 100)
        const mousewheelOption = this.graph.mousewheel.options
        this.zoomUpEnable = true
        this.zoomDownEnable = true
        if (this.zoomValue >= mousewheelOption.maxScale * 100) {
          this.zoomUpEnable = false
        }
        if (this.zoomValue <= mousewheelOption.minScale * 100) {
          this.zoomDownEnable = false
        }
      })
      this.graph.history.on('change', () => {
        this.undoEnable = this.graph.canUndo()
        this.redoEnable = this.graph.canRedo()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.ruleChain-tools {
  display: flex;
  position: absolute;
  top: 16px;
  left: 16px;
  z-index: 1000;

  .tool-item {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 48px;
    margin-right: 16px;
    background: #fff;
    border-radius: 2px;
    box-shadow: 0 1px 6px rgba(0, 0, 0, 0.1);
    .icon {
      width: 22px;
      height: 22px;
      margin: 0 14px;
      line-height: 48px;
      text-align: center;
      cursor: pointer;
      user-select: none;
      color: #666;
      &:last-child {
        .split-line {
          right: initial;
          left: 0;
        }
      }
      &.disabled {
        color: #ccc;
      }
    }
  }
  .split-line {
    width: 1px;
    height: 16px;
    background: #eee;
  }
  .scale-bar {
    .zoom-value {
      width: 80px;
      height: 48px;
      line-height: 48px;
      text-align: center;
      user-select: none;
      cursor: pointer;
    }
  }
  .history-bar{
    .icon {
      width: 16px;
      height: 16px;
    }
  }
}
</style>
